package com.hjy.test;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;

import java.io.IOException;
import java.net.InetSocketAddress;

public class MyClient {
    public static void main(String[] args) throws InterruptedException, IOException {
        //添加异步 Future
        ChannelFuture channelFuture = new Bootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel channel) throws Exception {
                        channel.pipeline().addLast(new StringEncoder());
                    }
                })
                //该方法是异步非阻塞 主线程调用后不阻塞
                //Nio 线程：NioEventLoop 中的线程
                .connect(new InetSocketAddress("localhost", 8080));
        //该方法用于等待连接
//        如果我们去掉channelFuture.sync()方法，会服务器无法收到hello world
//        channelFuture.sync();
        //还有一种是 添加 监听
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                Channel channel = channelFuture.channel();
                channel.writeAndFlush("hello1 ");
            }
        });
        System.in.read();
    }
    void syncTest() throws InterruptedException, IOException {
        //添加异步 Future
        ChannelFuture channelFuture = new Bootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel channel) throws Exception {
                        channel.pipeline().addLast(new StringEncoder());
                    }
                })
                //该方法是异步非阻塞 主线程调用后不阻塞
                //Nio 线程：NioEventLoop 中的线程
                .connect(new InetSocketAddress("localhost", 8080));
        //该方法用于等待连接
//        如果我们去掉channelFuture.sync()方法，会服务器无法收到hello world
        //还有一种是 添加 监听
        channelFuture.sync();
        //获取客户端和服务器之间的 channel 对象
        Channel channel = channelFuture.channel();
        channel.writeAndFlush("test");
        System.in.read();
    }
    void test() throws IOException, InterruptedException {
        //未添加监听器
        Channel channel = new Bootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel channel) throws Exception {
                        channel.pipeline().addLast(new StringEncoder());
                    }
                })
                .connect(new InetSocketAddress("localhost", 8080))
                .sync()
                .channel();
        System.out.println(channel);
        //  此处打断点  运行多个客户端
        System.in.read();
    }
}
